package main

import (
	"io"
	"fmt"
	"os"
)

func CopyFile(dstName, srcName string) (written int64, err error) {
	src, err := os.Open(srcName)
	if err != nil {
		fmt.Println("Open ERROR", err)
		return
	}
	defer src.Close()
	dst, err := os.OpenFile(dstName, os.O_WRONLY|os.O_CREATE, 0644)
	if err != nil {
		fmt.Println("OpenFile ERROR", err)
		return
	}
	defer dst.Close()
	// 先依次把2个文件都打开，然后拷贝只要下面这一句
	return io.Copy(dst, src)
}

func main() {
	CopyFile("test_copy.txt", "test.txt")
	fmt.Println("文件拷贝完成")
}